<?php
/**
 * Created by PhpStorm.
 * User: william
 * Date: 16-8-31
 * Time: 下午2:44
 */
namespace V5;
class C_RefundOrderDetail extends \ResourceClass {
    public $isActive = true;
    public $remark = '退单详情列表';

    public function get(array $req, array $post, &$result){
        try {
            /* 授权验证【开始】 */
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }
            if (!array_key_exists('id', $req) || empty($req['id'])) {
                return 'require resource id';
            }
            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler ->getById($req['userType'],$req['uid']);
            if(!is_object($customer_cls) && $customer_cls->getEnterpriseId() ==false){
                return 'data error';
            }
            $enterprise_id = $customer_cls ->getEnterpriseId();
            if (\Biz\UserHandler::checkAuth($req['userType'],$req['uid'],$enterprise_id,$req['id'].$req['t'],$req['k']) == false) {
                return 'data illegal';
            }
            /* 授权验证【结束】 */
            if(!array_key_exists('uid',$req)||empty($req['uid'])){
                return 'require uid';//买家id
            }
            $refund_order_handler = new \Biz\RefundOrderHandler();
            $enterprise_customer_handler = new \Biz\EnterpriseCustomerHandler();
            $enterprise_handler = new \Biz\EnterpriseHandler();
            $refund_img_biz = new \Biz\RefundImgHandler();

            $refund_img_list = $refund_img_biz->getImgById($req['id']);
            $refund_order_list = $refund_order_handler->getRefundOrderDetailById($req['id']);
            $refund_order = $refund_order_handler->getRefundOrderById($req['id']);
            $refund_order_cls = '';
            $refund_order_info = '';
            $order_detail_list = array();
            if(!empty($refund_order_list)){
                foreach($refund_order_list as $refund_order_cls){
                    $tmp_order_detail_list = array();
                    $enterprise_customer_cls = $enterprise_customer_handler->getEnterpriseCustomerById(
                        $enterprise_id, $req['uid']
                    );
                    $refund_order_info = $refund_order_handler->getByIdAndEnterpriseId($req['id'],$enterprise_id);
                    $enterprise_cls = $enterprise_handler->getById($enterprise_id);
                    if ($enterprise_customer_cls instanceof \Cls\User\EnterpriseCustomer) {
                        $refund_order_cls->setEnterpriseCustomerInfo($enterprise_customer_cls);
                    }
                    if ($enterprise_cls instanceof \Cls\Enterprise\Enterprise) {
                        $refund_order_cls->setEnterpriseInfo($enterprise_cls);
                    }
                    $product_id = $refund_order_cls->getProductId();
                    $product_handler = new \Biz\ProductHandler();
                    $product_img_cls = $product_handler->getListByProductId($product_id);
                    if(!is_object($product_img_cls) && $product_img_cls !=false){
                        foreach($product_img_cls as $product_img){
                            if($product_img->getDefaultStatus() == 1){
                                $tmp_order_detail_list['product_img'] = 'http://image.freshqiao.com/'.$product_img->getUrl();
                            }else{
                                $tmp_order_detail_list['product_img'] = 'http://image.freshqiao.com/'.$product_img->getUrl();
                            }
                        }
                    }else{
                        $tmp_order_detail_list['product_img'] = '';
                    }
                    $tmp_order_detail_list['product_id'] = $product_id;
                    $tmp_order_detail_list['product_name'] = $refund_order_cls->getProductName();
                    $tmp_order_detail_list['product_sku_id'] = $refund_order_cls->getProductSkuId();
                    $tmp_order_detail_list['product_sku_attr'] = $refund_order_cls->getProductSkuAttr();
                    $tmp_order_detail_list['refund_sku_amout'] = $refund_order_cls->getRefundSkuAmout();
                    $tmp_order_detail_list['price'] = $refund_order_cls->getSkuInfo()->getSellingPrice();
                    if(is_object($refund_order_cls->getProductInfo() && $refund_order_cls->getProductInfo() !=false)){
                        $sell_unit_string = $refund_order_cls->getProductInfo()->getSellUnitString();
                    }
                    if(empty($sell_unit_string)){
                        $sell_unit_strings = '未设置';
                    }else{
                        $sell_unit_strings = $sell_unit_string;
                    }
                    $tmp_order_detail_list['sell_unit_string'] = $sell_unit_strings;
                    $order_detail_list['order_detail_list'][] = $tmp_order_detail_list;
                }
            }
            //获取退单状态流程
            if ($refund_order->getProcessId()) {
                $thread_hendler = new \Biz\WorkflowThreadHandler();
                $thread_list = $thread_hendler->getThreadlist($refund_order->getProcessId(), $refund_order->getEnterpriseId());
                if ($thread_list) {
                    foreach ($thread_list as $thread_cls) {
                        $thread = array();
                        $thread['id'] = $thread_cls->getId();
                        $thread['node_name'] = $thread_cls->getNodeName();
                        $thread['remark'] = $thread_cls->getRemark();
                        if ($thread_cls->getStatus() == \Cls\Workflow\Thread::THREAD_STATUS_NO || $thread_cls->getStatus() == \Cls\Workflow\Thread::THREAD_STATUS_YES) {
                            $thread['status'] = \Cls\Workflow\Thread::THREAD_STATUS_NO;
                            $thread['time'] = '';
                        } else {
                            $thread['status'] = \Cls\Workflow\Thread::THREAD_STATUS_YES;
                            $thread['time'] = $thread_cls->getFinishTime();
                        }
                        $order_detail_list['workflowList'][] = $thread;
                    }
                }
            }
            if(!array_key_exists('workflowList',$order_detail_list)){
                $order_detail_list['workflowList'] = array();
            }
            $order_detail_list['enterprise_name'] = $refund_order_cls->getEnterpriseInfo()->getName();
            $order_detail_list['order_code'] = $refund_order_info->getRefundOrderCode();
            $order_detail_list['insert_time'] = $refund_order_info->getCreateAt();
            $order_detail_list['status'] = $refund_order_info->getStatusStr();
            $order_detail_list['refund_price'] = $refund_order_info->getRefundPrice();
            $order_detail_list['apply_price'] = $refund_order_info->getRefundPrice();
            $order_detail_list['remark'] = $refund_order_info->getRemark();
            $order_detail_list['refund_remark'] = '';
            $order_detail_list['img_list'] = $refund_img_list;
            $result = $order_detail_list;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

}